
(function (mui, shiquan) {
	/*服务地址*/
  	shiquan.SERVERURL="http://120.76.205.241:8000";
  	/*ApiKey*/
  	shiquan.APIKEY = "EPuiCUCPQ6kS917aq2gZoaBpsOXtFzEgqdAqTJwRgRg8MHEYtnt0MBYJ04TiRzCM";
  	/*站点*/
  	shiquan.getSite = function(){
  		let site = localStorage.getItem("shiquan_site") || "qq.com";
		return site;
  	};
  	/*站点*/
  	shiquan.setSite = function(site){
  		localStorage.setItem("shiquan_site",site);
  	};
  	/*关键字*/
  	shiquan.getKeyword = function(){
  		let site = localStorage.getItem("shiquan_keyword") || "实全科技";
		return site;
  	};
  	/*关键字*/
  	shiquan.setKeyword = function(keyword){
  		localStorage.setItem("shiquan_keyword",keyword);
  	};
  	/*关键字*/
  	shiquan.getKeywords = function(){
  		let keyword = localStorage.getItem("shiquan_keywords");
  		if(keyword != undefined && keyword != "")
  			return JSON.parse(keyword);
  		return ["财经","科技","军事","体育","教育","文化","娱乐","美食"];
  	};
  	/*关键字*/
  	shiquan.setKeywords = function(keywords){
  		localStorage.setItem("shiquan_keywords",JSON.stringify(keywords));
  	};
  	
  	/*读取内容*/
  	shiquan.readNews = function(callback){
  		callback = callback || mui.noop;
  		
  		plus.io.requestFileSystem( plus.io.PRIVATE_DOC, function( fs ) {
			// fs.root是根目录操作对象DirectoryEntry
			//console.log("url="+fs.toURL());
			fs.root.getFile('shiquan.json',{create:true}, function(fileEntry){
				fileEntry.file( function(file){
					console.log(file.size + '--' + file.name);
					var fileReader = new plus.io.FileReader();
					console.log("getFile:" + JSON.stringify(file));
					fileReader.readAsText(file, 'utf-8');
					fileReader.onloadend = function(evt) {
						console.log("11" + evt);
						console.log("evt.target" + evt.target);
						console.log(evt.target.result);
						
						let news = [];
						var result = evt.target.result;
						if(result != "")
							news = JSON.parse(result);
						callback({state:1,message:"读取成功！",data:news});
					}
				});
			});
		}, function ( e ) {
			callback({state:-1,message:e.message});
		} );
  	};
  	/*添加内容*/
  	shiquan.saveNew = function(data,callback){
  		callback = callback || mui.noop;
  		var isAdd = true;
  		console.log(shiquan.news);
  		if(shiquan.news == undefined){
  			shiquan.news = []; 
  		}
  		shiquan.news.forEach(function(item) {
  			if(item.id == data.id)
  			{
  				isAdd = false;
  				return;
  			}
		});
		if(isAdd == false){
			callback({state:1,message:"数据已存在，不能重复保存！"});
			return;
		}
		shiquan.news.push(data);
  		//var fileName = item.id + ".json";
  		shiquan.saveNews(callback);
  	};
  	/*删除内容*/
  	shiquan.deleteNew = function(item){
  		
  	};
  	/*保存内容*/
  	shiquan.saveNews = function(news,callback){
  		callback = callback || mui.noop;
  		plus.io.requestFileSystem( plus.io.PRIVATE_DOC, function( fs ) {
			// fs.root是根目录操作对象DirectoryEntry
			//console.log("url="+fs.toURL());
			fs.root.getFile('shiquan.json',{create:true}, function(fileEntry){
				fileEntry.createWriter( function ( writer ) {
					writer.onwrite = function ( e ) {
						console.log( "Write data success!" );
					};
					let data = JSON.stringify(news);
					console.log(data);
					
					writer.seek(0);
					writer.write(data);
					callback({state:1,message:"保存数据成功！"});
				}, function ( e ) {
					callback({state:-1,message:e.message});
				} );
			});
		}, function ( e ) {
			callback({state:-1,message:e.message});
		} );
  	};
}(mui,window.app = {}));